home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / SQLFUNCS.C < prev    next >
Text File  |  1993-06-10  |  7KB  |  333 lines

  1. /**************************************************
  2. * FILE NAME:  SQLfuncs.c   TITLE:  SQL database API
  3. *
  4. * AUTHOR:  K. E. North II
  5. *
  6. * SYNOPSIS: 
  7. *
  8. *    application interface to Pioneer's Q+E Lib
  9. *
  10. ***************************************************/
  11.  
  12. #undef DEBUG    
  13. #define    __QEDBF
  14.  
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <dos.h>
  18. #include <stdlib.h>
  19. #include <conio.h>
  20. #include <string.h>
  21. #include <alloc.h>
  22.  
  23. #ifndef __QEDEFS_H
  24.         /*QELib-specific definitions */
  25. #include "QEdefs.h"
  26. #endif
  27.  
  28. #ifndef __QEAPI_H
  29.         /* function prototypes for QELib calls */
  30. #include "QEapi.h"
  31. #endif
  32.  
  33. #ifndef __QCOLUMN_H
  34.         /* QELib column description */
  35. #include "qcolumn.h"      
  36. #endif
  37.  
  38. #ifndef __DBTYPES_H
  39. #include "dbtypes.h"
  40. #endif
  41.  
  42. #ifndef __REQUEST_H
  43. #include "request.h"
  44. #endif
  45.  
  46. #ifndef __SQLFUNCS_H
  47.         /* portable SQL API functions */
  48. #include "sqlfuncs.h"
  49. #endif
  50.  
  51.  
  52. /**************************************************
  53. * FUNCTION NAME: dbClose
  54. *
  55. * SYNOPSIS:
  56. *
  57. *  close a data base session - terminate connection
  58. *
  59. ***************************************************/
  60. int dbClose(CONNECTION si)
  61. {
  62. int        ExitStatus;
  63.  
  64.                             /* to logout */
  65.     ExitStatus = dbDisconnect( si );
  66.     if (ExitStatus)
  67.         return ExitStatus;
  68.     else 
  69.         return SQL_SUCCESS;
  70. }
  71.  
  72.  
  73. /**************************************************
  74. * FUNCTION NAME: dbRequest
  75. *
  76. * SYNOPSIS: 
  77. *
  78. *     prep/exec an SQL statement 
  79. *     for Q+E Lib
  80. *
  81. ***************************************************/
  82. int dbRequest( REQUEST rq, char *statement,
  83.                    CONNECTION si )
  84. {
  85.  
  86. HSTMT    hstmt;
  87. POINTER    st;
  88. int          StatementLength;
  89.  
  90.                     /* get length of */
  91.                     /* SQL command string */
  92.     StatementLength = strlen (statement);
  93.     if (StatementLength > MAXLEN)
  94.         {
  95.         rq->ExceedsMax=TRUE;
  96.         }
  97.     else rq->ExceedsMax = FALSE;
  98.  
  99.         /* copy 1st SQL string into request */
  100.     strcpy(rq->Statement,statement);
  101.  
  102.         /* build FAR * SQL request */
  103.     st = BuildRequest( rq );
  104.     rq->stptr = st;
  105.  
  106.     hstmt = ExecuteStatement( st, si );
  107.  
  108.         /* if there is an error, hstmt is  a 0     */
  109.         /* or negative number                     */
  110.     if (hstmt <= NOHANDLE)
  111.         {
  112.         return REQUEST_ERROR;
  113.         }
  114.     rq->hstmt = hstmt;    /* save request handle */
  115.     return SQL_SUCCESS;
  116. }
  117.  
  118. /**************************************************
  119. * FUNCTION NAME: BuildRequest
  120. *
  121. * SYNOPSIS: prepare an SQL request
  122. ***************************************************/
  123. POINTER BuildRequest( REQUEST rq )
  124. {
  125.  
  126. POINTER stptr;
  127.  
  128.     stptr = (char far *) farmalloc(MAXSQL);
  129.     movedata
  130.      (FP_SEG(rq->Statement), FP_OFF(rq->Statement),
  131.             FP_SEG(stptr), FP_OFF(stptr),
  132.             1+strlen(rq->Statement));
  133.     return stptr;
  134. }
  135.  
  136. /**************************************************
  137. * FUNCTION NAME: ExecuteStatement
  138. *
  139. * SYNOPSIS: executes a direct or prepared request
  140. **************************************************/
  141. HSTMT ExecuteStatement( POINTER stptr, 
  142.                         CONNECTION si )
  143. {
  144. HSTMT    hstmt;
  145. int        Status;
  146.  
  147.     hstmt = qeExecSQL (si->hdbc, stptr);
  148.     if (hstmt == 0)
  149.         {
  150.         Status = qeErr();
  151.         return Status;
  152.         }
  153.     return hstmt;
  154. }
  155.  
  156. /**************************************************
  157. * FUNCTION NAME: dbDescribe
  158. *
  159. * SYNOPSIS:     
  160. *
  161. *
  162. ***************************************************/
  163. int dbDescribe(int ColumnPos,
  164.                 struct ColumnDesc *col,
  165.                 REQUEST rq)
  166. {
  167. char    far *name;
  168. int      Status;
  169.  
  170.     col->namelen = NAMELENGTH;
  171.     col->data_type = 0;
  172.     col->length = 0;
  173.     col->scale = 0;
  174.     strcpy(col->fldname,"   ");
  175.  
  176.          /* get the column name, type, width, etc. */
  177.     name = qeColName ( rq->hstmt, ColumnPos );
  178.     Status = qeErr();
  179.     if (Status == SQL_SUCCESS)
  180.         {
  181.         strcpy(col->fldname,name);
  182.         }
  183.     else return Status;
  184.  
  185.                 /* get the column type */
  186.     col->data_type = qeColType ( rq->hstmt, 
  187.                     ColumnPos );
  188.     Status = qeErr();
  189.     if (Status != SQL_SUCCESS)
  190.         return Status;
  191.  
  192.                 /* get the column width */
  193.     col->length = qeColWidth ( rq->hstmt,
  194.                             ColumnPos );
  195.     Status = qeErr();
  196.     if (Status == SQL_SUCCESS)
  197.         {
  198.         if (col->data_type == CHARACTER)
  199.             --col->length;
  200.         }
  201.     else return Status;
  202.  
  203.     if (col->data_type == DECIMAL)
  204.         {
  205.         col->scale = qeColScale ( rq->hstmt,
  206.                             ColumnPos );
  207.         Status = qeErr();
  208.         if (Status != SQL_SUCCESS)
  209.             return Status;
  210.         }
  211.  
  212.     return SQL_SUCCESS;
  213. }
  214.  
  215. /**************************************************
  216. * FUNCTION NAME: dbConnect
  217. *
  218. * SYNOPSIS:
  219. *             open an database connection
  220. ***************************************************/
  221.  
  222. int dbConnect( CONNECTION si, DATASRC dsrc)
  223. {
  224. int        Status;
  225.  
  226.                 /********************************/
  227.                 /*   Connect to Q+E database    */
  228.                 /********************************/
  229.  
  230.     si->cstr = BuildConnectString(dsrc);
  231.  
  232.             /* QELib connect */
  233.     si->hdbc  = qeConnect (si->cstr);
  234.     Status = qeErr();
  235.     if (si->hdbc == 0)
  236.       {
  237.       #ifdef DEBUG
  238.       printf ("<Connect> qeConnect failure, 
  239.                 Status: %d\n", Status);
  240.       #endif
  241.       return Status;
  242.       };
  243.     return SQL_SUCCESS;
  244. }
  245.  
  246.  
  247. /**************************************************
  248. * FUNCTION NAME: dbDisconnect
  249. *
  250. * SYNOPSIS: 
  251. *        terminate a data base connection
  252. ***************************************************/
  253. int dbDisconnect(CONNECTION si)
  254. {
  255. int      Status;
  256.  
  257.             // QELib disconnect
  258.     Status = qeDisconnect(si->hdbc);
  259.     if (Status)
  260.         return Status;
  261.     else
  262.         return SQL_SUCCESS;
  263.  
  264. }
  265.  
  266. /**************************************************
  267. * FUNCTION NAME: BuildConnectString
  268. *
  269. * SYNOPSIS: build connection string for DBF
  270. ***************************************************/
  271. POINTER BuildConnectString(DATASRC dsrc)
  272. {
  273.     char far *conec;
  274.     char *str="DRV=";
  275.  
  276.       /* to connect to a DBF database, supply only
  277.             the driver parameter */
  278.     strcat(str,dsrc->DBDriver);
  279.  
  280.     conec = (char far *) farmalloc(CONECLEN);
  281.     movedata(FP_SEG(str), FP_OFF(str),
  282.         FP_SEG(conec), FP_OFF(conec),
  283.         strlen(str));
  284.     return conec;
  285. }
  286.  
  287. /**************************************************
  288. * FUNCTION NAME: DecodeQEDataType
  289. *
  290. * SYNOPSIS: return description of data type
  291. ***************************************************/
  292.  
  293. char *DecodeQEDataType(int DataType)
  294. {
  295. static char Type[20];
  296.  
  297.     strcpy(Type,"Unknown");
  298.     if (DataType == 1)
  299.         strcpy(Type,"character");
  300.     if (DataType == 2)
  301.         strcpy(Type,"variable len char");
  302.     if (DataType == 3)
  303.         strcpy(Type,"decimal");
  304.     if (DataType == 4)
  305.         strcpy(Type,"long integer");
  306.     if (DataType == 5)
  307.         strcpy(Type,"integer");
  308.     if (DataType == 6)
  309.         strcpy(Type,"float");
  310.     if (DataType == 7)
  311.         strcpy(Type,"double");
  312.     if (DataType == 8)
  313.         strcpy(Type,"date-time");
  314.     return Type;
  315. }
  316.  
  317. /**************************************************
  318. * FUNCTION NAME: FindNumberofColumns
  319. *
  320. * SYNOPSIS: 
  321. *    get the number of columns in the request
  322. ***************************************************/
  323.  
  324. int FindNumberOfColumns( HSTMT hstmt )
  325. {
  326. int        n;
  327.  
  328.     n = qeNumCols (hstmt);
  329.     return n;
  330. }
  331.  
  332.  
  333.